package sample.plugin.html.project.template;

import android.graphics.drawable.Drawable;

import com.rapid.api.ext.plugin.AndroidPlugins;
import com.rapid.api.project.BaseTemplate;
import com.rapid.api.project.Project;
import com.rapid.framework.data.form.Form;
import com.rapid.framework.data.form.FormInput;
import com.rapid.util.android.AssetUtils;
import com.rapid.util.file.FileUtils;

import java.io.File;
import java.io.IOException;

import sample.plugin.html.project.HtmlProject;

/**
 * 默认模板类
 */
public class HtmlDefaultTemplate extends BaseTemplate<HtmlProject> {
    public static final String TEMPLATE_NAME = "默认模板";
    public static final String DEFAULT_PROJECT_NAME = "我的Html工程";
    public static final String KEY_USE_JQUERY = "useJQuery";

    @Override
    public String getName() {
        return TEMPLATE_NAME;
    }

    @Override
    public Drawable getPicture() {
        return null;
    }

    @Override
    public String getDefaultProjectName() {
        return DEFAULT_PROJECT_NAME;
    }

    @Override
    public Form getForm() {
        Form form = new Form();
        FormInput useJQuery = new FormInput(
                FormInput.Kind.BOOLEAN,
                KEY_USE_JQUERY,
                "使用JQuery",
                "是否使用JQuery"
        );
        form.addInput(useJQuery);
        return form;
    }

    @Override
    public void create(HtmlProject project, Form form, Project.OnProjectCreateListener onProjectCreateListener) {
        //获取是否要使用JQuery
        boolean useJQuery = form.getInput(KEY_USE_JQUERY).getBooleanValue();
        File file = project.getChildFile("index.html");
        try {
            String html = AssetUtils.readAsset(AndroidPlugins.getContext(this), "sample/index.html");
            html = html.replace("${project_name}", project.getName());
            FileUtils.writeFile(html, file);
            if (useJQuery) {
                //TODO 将JQuery解压到指定目录
            }
            onProjectCreateListener.onSuccess();
        } catch (IOException e) {
            e.printStackTrace();
            onProjectCreateListener.onFail("写出默认文件失败");
        }
    }

}
